package arithmetic.multithread;

/**
 * 线程间通信方式一
 * @author lidd
 *
 */
public class Thread1 {

	public static void main(String[] args) {
		
		final Printer print = new Printer();
		
		new Thread() {
			public void run() {
				while(true) {
					try {
						print.print1();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
		
		new Thread() {
			public void run() {
				while(true) {
					try {
						print.print2();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
	}

}

class Printer{	
	private int flag = 1;
	
	public void print1() throws InterruptedException {
		synchronized(this) {
			if(flag != 1) {
				this.wait();
			}
			System.out.print("��");
			System.out.print("��");
			System.out.print("��");
			System.out.print("��");
			System.out.print("��");
			System.out.print("��");
			System.out.println("\r\n");
			flag = 2;
			this.notify();
		}
	}
	
	public void print2() throws InterruptedException {
		synchronized(this) {
			if(flag != 2) {
				this.wait();
			}
			System.out.print("��");
			System.out.print("��");
			System.out.print("��");
			System.out.print("��");
			System.out.print("��");
			System.out.print("��");
			System.out.println("\r\n");
			flag = 1;
			this.notify();
		}
	}
	
}